//
//  UserAccountViewModel.swift
//  SinaWeibo
//
//  Created by mac on 15/12/15.
//  Copyright © 2015年 cn.bboy-Deven. All rights reserved.
//

import UIKit
import AFNetworking

class UserAccountViewModel: NSObject {
    var userAccount : UserAccountInfo?
    
    override init(){
        userAccount = UserAccountInfo.loadAccount()
        super.init()
    }
  
    // 判断用户是否登陆 
    var userLogin : Bool {
        return userAccount?.access_token != nil
    }
    
    
    
    // 获取 token  需要 afn 发送请求
    func getAccessToken(code:String, finished: (isSuccess: Bool) -> ()){
        
        let httpUrl = "https://api.weibo.com/oauth2/access_token"
        let parameters = ["client_id":client_id,"client_secret":client_secret,"grant_type":"authorization_code","code":code,"redirect_uri":redirect_uri]
        // AFN 发送请求  --- 得到的结果 中 就有 token , 然后进行 处理
        //            let AFN = AFURLSessionManager()
        let AFN = AFHTTPSessionManager()
        AFN.responseSerializer.acceptableContentTypes?.insert("text/plain")
        AFN.POST(httpUrl, parameters: parameters, progress: { (p) -> Void in
            print(p)
            }, success: { (_, result) -> Void in
                //  获取 token  成功
                print(result)
                // 判断 result  是否有值, 如果
                if let dict = result as? [String : AnyObject] {
                    
                    let UserAccount = UserAccountInfo(dict:dict)
                    // 上面这句  字典转模型 完成,  模型里面有了 token 的三个 字段的数据
                    self.getUserInfo(UserAccount,finished: finished)
                    print(UserAccount)
                }
                
            }) { (_, error) -> Void in
                print(error)
                finished(isSuccess: false)
                
        }
    }
    
    // 获取用户数据  --- 对 AFN 得到的结果进行处理
    private func getUserInfo(UserAccount : UserAccountInfo, finished: (isSuccess: Bool) -> ()) {
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        // 这个判断 让 parameters 不再是可选, 确定了 类型
        if let token = UserAccount.access_token , userId = UserAccount.uid{
            let parameters = ["access_token":token ,"uid":userId]
            let AFN = AFHTTPSessionManager()
            AFN.GET(urlString, parameters: parameters, progress: { (_) -> Void in
                
                }, success: { (_, result) -> Void in
                    print(result)
                    // 获得 用户数据, 进行 后续处理
                    if let dict = result {
                        let avatar_large = dict["avatar_large"] as! String
                        let name = dict["name"] as! String
                        
                        // 直接给 模型属性 赋值
                        UserAccount.name = name
                        UserAccount.avatar_large = avatar_large
                        
                        // 这里要归档 
                        UserAccount.saveAccount()
                        print(avatar_large,name)
                        finished(isSuccess: true)
                    }
                    
                }) { (_, error) -> Void in
                    print(error)
                    finished(isSuccess: false)
            }
        }
        
        
    }
    
    
    
}
